package org.sea.mybator.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class ParseSqlTypeToJavaType   {
    private Properties prop = null;
    private Map<String, String> map = new HashMap<String, String>();
    private String paramLocation;
    private String propertiyKey = "sqlTypeTojavaType";

    public ParseSqlTypeToJavaType() {
    }

    public ParseSqlTypeToJavaType(String paramLocation) {
        this.paramLocation = paramLocation;
    }

    public ParseSqlTypeToJavaType(String paramLocation, String propertiyKey) {
        this.paramLocation = paramLocation;
        this.propertiyKey = propertiyKey;
    }

    public boolean init() {
        try {
            this.paramLocation = (this.paramLocation == null ? "/dataTypeConversion.properties" : this.paramLocation);
            InputStream path = getClass().getResourceAsStream(this.paramLocation);
            if (path == null) {
                System.err.println("ParseSqlTypeToJavaType:未找到指定的配置文件 " + this.paramLocation);
                return false;
            }
            this.prop = new Properties();
            this.prop.load(path);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean parse() {
        if (this.prop == null) {
            System.err.println("ParseSqlTypeToJavaType：找不到" + this.paramLocation + "文件对象！");
            return false;
        }
        String sqlTypeTojavaType = (String) this.prop.get(this.propertiyKey);
        if ((sqlTypeTojavaType != null) && (!"".equals(sqlTypeTojavaType))) {
            String[] types = sqlTypeTojavaType.split(",");
            if ((types != null) && (types.length > 0)) {
                for (String type : types) {
                    String[] units = type.split(":");
                    if ((units != null) && (units.length == 2)) {
                        this.map.put(units[0].substring(1), units[1].substring(0, units[1].length() - 1));
                    } else
                        System.err.println("ParseSqlTypeToJavaType：" + this.paramLocation + "中sqlTypeTojavaType(" + type
                                + ")属性对象，配置有误！");
                }
            }
        } else {
            System.err.println("ParseSqlTypeToJavaType：" + this.paramLocation + "中找不到sqlTypeTojavaType属性对象！");
            return false;
        }
        return true;
    }

    public Properties getProp() {
        return this.prop;
    }

    public Map<String, String> getMap() {
        if ((this.map == null) || (this.map.size() == 0)) {
            parse();
        }
        return this.map;
    }

    public Map<String, String> getMap(String paramLocation) {
        if ((this.map == null) || (this.map.size() == 0)) {
            this.paramLocation = paramLocation;
            parse();
        }
        return this.map;
    }

    public String getParamLocation() {
        return this.paramLocation;
    }

    public String getPropertiyKey() {
        return this.propertiyKey;
    }

    public void setPropertiyKey(String propertiyKey) {
        this.propertiyKey = propertiyKey;
    }

    public static void main(String[] args) {
    }
}
